package com.gaohe.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.mapper.SetMealMapper;
import com.gaohe.pojo.Category;
import com.gaohe.pojo.Setmeal;
import com.gaohe.pojo.SetmealDish;
import com.gaohe.pojo.SetmealDto;
import com.gaohe.service.CategoryService;
import com.gaohe.service.SetMealDishService;
import com.gaohe.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetMealServiceImp extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetMealDishService setMealDishService;


    @Override
    public Page<SetmealDto> PAGE_R(Integer page, Integer pageSize, String name) {
        //查询setMeal表的数据
        Page<Setmeal> page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper();
        lqw.like(name != null, Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> setMealPage = this.page(page1, lqw);
        //复制到setMealDto表中
        Page<SetmealDto> setMealDtoPage = new Page();
        BeanUtils.copyProperties(setMealPage, setMealDtoPage, "records");
        //将categoryName加入到setMealPage的records中,再添加给setMealDtoPage
        List<Setmeal> records = setMealPage.getRecords();
        //获取setMealDtoPage的records的集合
        List<SetmealDto> collect = records.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Long categoryId = setmeal.getCategoryId();
            Category categoryName = categoryService.getById(categoryId);
            setmealDto.setCategoryName(categoryName.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setMealDtoPage.setRecords(collect);
        return setMealDtoPage;
    }

    @Override
    @Transactional
    public void insertSetMeal(SetmealDto setmealDto) {
        //新增两个表的数据：setMeal表与setMeal_dish中间表表
        //插入setMeal表数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        this.save(setmeal);
        //向setmealDishes中加入setMealId然后再插入数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map(dish -> {
            dish.setSetmealId(setmeal.getId());
            return dish;
        }).collect(Collectors.toList());
        setMealDishService.saveBatch(setmealDishes);

    }

    @Override
    @Transactional
    public void updateSetMeal_Dish(SetmealDto setmealDto) {
        //修改两个表:setMeal表与setMealDish表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        this.updateById(setmeal);
        //setMealDish表:先删除再新增
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper();
        lqw.eq(SetmealDish::getSetmealId, setmeal.getId());
        setMealDishService.remove(lqw);
        //为setmealDishes里加入setMealId
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map(setMealDish -> {
            setMealDish.setSetmealId(setmeal.getId());
            return setMealDish;
        }).collect(Collectors.toList());
        setMealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    @Transactional
    public void stop(Integer id, String ids) {
        //处理接收的ids
        String[] split = ids.split(",");
        List<String> stringList = Arrays.asList(split);
        //根据ids来修改status为0
        //先查后改
        List<Setmeal> setmeals = this.listByIds(stringList);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(id);
        }
        this.updateBatchById(setmeals);

    }

    @Override
    @Transactional
    public void deleteSetMealDish(String ids) {
        //处理接收的数据
        String[] split = ids.split(",");
        List<String> stringList = Arrays.asList(split);
        //删除两张表的内容:setMeal表与setMealDish表
        //先查再删
        //删除setMeal表
        List<Setmeal> setmeals = this.listByIds(stringList);
        for (Setmeal setmeal : setmeals) {
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper();
            lqw.eq(Setmeal::getId, setmeal.getId());
            this.remove(lqw);
        }
        //删除setMealDish表
        for (String s : stringList) {
            LambdaQueryWrapper<SetmealDish> lqw1 = new LambdaQueryWrapper();
            lqw1.eq(SetmealDish::getSetmealId, s);
            setMealDishService.remove(lqw1);
        }


    }
}
